package com.example.task1.ui.AllService.traffic;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.TrafficCartListBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class TrafficModificationCartActivity extends BaseActivity {

    private int mId;
    private EditText tvPlateNo;
    private EditText tvEngineNo;
    private EditText tvType;
    private Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traffic_modification_cart);
        mId = getIntent().getIntExtra("id", 0);
        initView();
        business();
    }

    @Override
    public String getMyTitle() {
        return mId == 0 ? "修改车辆信息" : "新增车辆信息";
    }


    private void business() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String engineNo = tvEngineNo.getText().toString();
                String plateNo = tvPlateNo.getText().toString();
                String type = tvType.getText().toString();
                if (TextUtils.isEmpty(engineNo)) {
                    tvEngineNo.setError("不能为空");
                } else if (TextUtils.isEmpty(plateNo)) {
                    tvPlateNo.setError("不能为空");
                } else if (TextUtils.isEmpty(type)) {
                    tvType.setError("不能为空");
                } else {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("engineNo", engineNo);
                    map.put("plateNo", plateNo);
                    map.put("type", type);
                    if (mId == 0) {
                        map.put("id", MyApplication.Traffic.cartDate.getId());
                        Call<ApiResp> put = Api.api().put(MyApplication.TOKEN, "/prod-api/api/traffic/car", Api.getBody(map));
                        Api.send(put, ApiResp.class, new CallBack<ApiResp>() {
                            @Override
                            public void success(ApiResp data) {
                                intent("修改成功");
                            }

                            @Override
                            public void error(String msg) {

                            }
                        });
                    } else {
                        Call<ApiResp> put = Api.api().post(MyApplication.TOKEN, "/prod-api/api/traffic/car", Api.getBody(map));
                        Api.send(put, ApiResp.class, new CallBack<ApiResp>() {
                            @Override
                            public void success(ApiResp data) {
                                intent("修改成功");
                            }

                            @Override
                            public void error(String msg) {

                            }
                        });
                    }
                }

            }
        });
    }

    private void intent(String msg) {
        MyApplication.show(msg);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.Traffic.cartDate = null;
    }

    private void initView() {
        tvPlateNo = findViewById(R.id.tv_plateNo);
        tvEngineNo = findViewById(R.id.tv_engineNo);
        tvType = findViewById(R.id.tv_type);
        btnSave = findViewById(R.id.btn_save);
        if (mId == 0) {
            TrafficCartListBean.RowsDTO cartDate = MyApplication.Traffic.cartDate;
            tvEngineNo.setText(cartDate.getEngineNo());
            tvPlateNo.setText(cartDate.getPlateNo());
            tvType.setText(cartDate.getType());
        }
    }
}